//
//  MainClassGroupCellelement.m
//  cherryproduct
//
//  Created by umessage on 14-8-9.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "MainClassGroupCellelement.h"
#import "UIImageView+AFNetworking.h"

@interface MainClassGroupCellelement ()

@property (nonatomic, strong) UIImageView * imageview_bg;
@property (nonatomic, strong) UIButton * button_0;

@end

@implementation MainClassGroupCellelement

@synthesize delegate;
@synthesize maintag;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        if (self.imageview_bg == nil) {
            self.imageview_bg = [[UIImageView alloc] initWithFrame:self.frame];//背景图片
            self.imageview_bg.contentMode=UIViewContentModeScaleAspectFit;
            [self addSubview:self.imageview_bg];
        }
        
        if (self.button_0 == nil) {
            self.button_0 = [[UIButton alloc] init];
            
            self.button_0.frame = CGRectMake(10, self.frame.size.height-74-10, 74, 74);
            self.button_0.titleLabel.numberOfLines = 0;
            [self.button_0 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.button_0.titleLabel.font = [UIFont systemFontOfSize:22];
            self.button_0.titleLabel.textAlignment = NSTextAlignmentCenter;
            
            self.button_0.layer.cornerRadius = 37;
            self.button_0.layer.masksToBounds = YES;
            self.button_0.layer.borderWidth = 0.0;
            self.button_0.layer.borderColor = [UIColor whiteColor].CGColor;
            [self.button_0 setTitleEdgeInsets:UIEdgeInsetsMake(0,7,0,7)];
            self.button_0.tag = -1;
            [self.button_0 addTarget:self action:@selector(toucdownbutton:) forControlEvents:UIControlEventTouchUpInside];
            
            [self addSubview:self.button_0];
        }
        
        if (self.scrollview_base == nil) {
            CGFloat y = self.frame.size.height-40-10;
            CGFloat widthPP = self.frame.size.width-90;
            
            self.scrollview_base=[[UIScrollView alloc] initWithFrame:CGRectMake(89, y, widthPP, 40)];
            self.scrollview_base.backgroundColor=[UIColor clearColor];
            self.scrollview_base.showsHorizontalScrollIndicator=NO;
            self.scrollview_base.showsVerticalScrollIndicator=NO;
            [self addSubview:self.scrollview_base];
            
            UIImage * imagesec = [UIImage imageNamed:@"classmainviewxihusec"];
            
            for (int i = 0; i < 30; i++) {
                UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(i*50, 0, 40, 40)];
                btn.titleLabel.numberOfLines = 0;
                [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:11];
                btn.titleLabel.textAlignment = NSTextAlignmentCenter;
                [btn setTitleEdgeInsets:UIEdgeInsetsMake(0,7,0,7)];
                
                btn.layer.cornerRadius = 20;
                btn.layer.masksToBounds = YES;
                btn.layer.borderWidth = 1.0;
                btn.layer.borderColor = [UIColor redColor].CGColor;
                
                [btn setBackgroundImage:imagesec forState:UIControlStateNormal];
                btn.tag = i+10;
                [btn addTarget:self action:@selector(toucdownbutton:) forControlEvents:UIControlEventTouchUpInside];
                
                UIImageView * line = [[UIImageView alloc] initWithFrame:CGRectMake(2, 18.5, 40-4, 0.5)];
                line.backgroundColor=[UIColor grayColor];
                line.tag=888;
                [btn addSubview:line];
                
                [self.scrollview_base addSubview:btn];
            }
        }
    }
    return self;
}

-(void)getusedata:(NSDictionary*)dic
{
    if (dicdata) {
        [dicdata removeAllObjects];
    }
    dicdata = [[NSMutableDictionary alloc] initWithDictionary:dic copyItems:YES];
    
    if (self.imageview_bg) {
        NSMutableString * stringimageurl = [[NSMutableString alloc] init];
        [stringimageurl appendString:JPG];
        [stringimageurl appendString:[dicdata objectForKey:@"img"]];
        [self.imageview_bg setImageWithURL:[NSURL URLWithString:stringimageurl]
                          placeholderImage:[UIImage imageNamed:@"koubeiloading"]];
    }
    
    if (self.button_0) {
        [self.button_0 setTitle:[dicdata objectForKey:@"title"] forState:UIControlStateNormal];
        NSString * str_color = [dicdata objectForKey:@"color"];
        if (str_color && [str_color isKindOfClass:[NSString class]] && ![str_color isEqualToString:@""]) {
            UIColor * co = [FIConstMethod colorWithHexString:str_color withAlpha:0.7];
            self.button_0.backgroundColor=co;
        } else {
            self.button_0.backgroundColor=[UIColor whiteColor];
        }
    }
    
    NSArray * array_s_class = [dicdata objectForKey:@"s_class"];
    
    if (array_s_class && [array_s_class isKindOfClass:[NSArray class]]) {
        NSInteger pun = [array_s_class count];
        if (pun>30) {
            pun=30;
        }
        
        for (int i = 0; i < pun; i++) {
            NSDictionary * dic_item = [array_s_class objectAtIndex:i];
            
            UIButton * btn = (UIButton*)[self.scrollview_base viewWithTag:i+10];
            
            if (btn && [btn isKindOfClass:[UIButton class]]) {
                btn.hidden=NO;
                
                NSString * str_name = [dic_item objectForKey:@"title"];
                [btn setTitle:str_name forState:UIControlStateNormal];
                
                NSString * str_color = [dicdata objectForKey:@"color"];
                if (str_color && [str_color isKindOfClass:[NSString class]] && ![str_color isEqualToString:@""]) {
                    UIColor * co = [FIConstMethod colorWithHexString:str_color withAlpha:1.0];
                    btn.layer.borderColor = co.CGColor;
                } else {
                    btn.layer.borderColor = [UIColor orangeColor].CGColor;
                }
            
                if ([str_name length] > 2) {
                    UIView * line = [btn viewWithTag:888];
                    line.hidden=NO;
                } else {
                    UIView * line = [btn viewWithTag:888];
                    line.hidden=YES;
                }
            }
        }
        for (int i = pun; i < 30; i++) {
            UIButton * btn = (UIButton*)[self.scrollview_base viewWithTag:i+10];
            if (btn && [btn isKindOfClass:[UIButton class]]) {
                btn.hidden=YES;
            }
        }
        
        self.scrollview_base.contentSize=CGSizeMake([array_s_class count]*50+10, 40);
    }
}


- (void)toucdownbutton:(UIButton*)button
{
    int row = button.tag;
    if (row!=-1) {
        row=row-10;
    }
    if (row<0) {
        //row=0;
    }
    [self.delegate touchdownMainClassGroupCellelementindex:self.maintag numrow:row];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end


























